package cn.wit.summit.chwan.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.response.SearchAutoResponse;

/**
 * Created by tangbl on 2018/04/18.
 */

public class SearchAutoAdapter extends BaseAdapter {

    private List<SearchAutoResponse> mList;
    private Context context;

    public SearchAutoAdapter(Context context) {
        this.context = context;
        this.mList = new ArrayList<>();
    }

    public void setmList(List<SearchAutoResponse> mList) {
        this.mList.clear();
        this.mList.addAll(mList);
        notifyDataSetChanged();
    }

    public void clearData() {
        this.mList.clear();
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public SearchAutoResponse getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder viewHolder = null;

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_search_auto_view, null);
            viewHolder = new ViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.itemSearchAutoTx.setText(mList.get(position).getGame_name());

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.itemSearchAutoTx)
        TextView itemSearchAutoTx;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
